// pages/in/in.js
import {
  async
} from '../../utils/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img:[],
    imgs:[],
    imgss:[],
    imgList: [],
    imgLists: [],
    imgListss: [],
    upID: "",
    downID: '',
    modalName: '',
    fixContent: '',
    value: "",
    zhaopian: [],
    shiping: [],
    b: 0,
    leis: ['电控', '电池包', '驱动电机'],
    leixing: '电控',
    i: 0,
    carID: ''
  },

  /**
   * 选择器
   * @param {*} e 
   */
  DateChange(e) {
    // this.data.item.leixing = e.detail.value
    console.log(e)
    this.setData({
      leixing: this.data.leis[e.detail.value],
      i: e.detail.value
    })

  },
  /**
   * 赋值
   * @param {*} e 
   */
  changes(e) {
    this.setData({
      value: e.detail.value
    })
  },
  /**
   * 改变变量值
   * @param {*} e 
   */
  change(e) {
    if (this.data.modalName == '换上码') {
      this.setData({
        upID: this.data.value,
        value: ''
      })
    } else if (this.data.modalName == '换下码') {
      this.setData({
        downID: this.data.value,
        value: ''
      })
    } else {
      this.setData({
        carID: this.data.value,
        value: ''
      })
    }
    this.setData({
      modalName: ''
    })
  },
  /**
   * 点击弹窗
   * @param {*} e 
   */
  onTap(e) {
    console.log(e.currentTarget.dataset.id)
    this.setData({
      modalName: e.currentTarget.dataset.id
    })
  },
  /**
   * 上传文件
   * @param {*} e 
   */
  up(url, k) {
    var that = this
    let MyFile = new wx.BaaS.File()
    let fileParams = {}
    let metaData = {}
    var img = this.data.img
    var imgs = this.data.imgs
    var imgss = this.data.imgss
    fileParams = {
      filePath: url,
      fileType: "image"
    }

    if (k == 1 && this.data.i != 2) {
      metaData = {
        categoryName: that.data.leixing + '换上码'
      }
    }

    if (k == 2 && this.data.i != 2) {
      metaData = {
        categoryName: that.data.leixing + '换下码'
      }
    }

    if (k == 1 && this.data.i == 2) {
      metaData = {
        categoryName: that.data.leixing + '发动机号'
      }
    }

    if (k == 2 && this.data.i == 2) {
      metaData = {
        categoryName: that.data.leixing + 'vin码'
      }
    }
    if (k == 3 ) {
      metaData = {
        categoryName: '车牌号'
      }
    }
    
    MyFile.upload(fileParams, metaData).then(res => {
      // 上传成功
      let data = res.data // res.data 为 Object 类型
      console.log(data)
      
      if (k == 1) {
        img.push(res.data.path)
        that.setData({
          img: img,
          b: that.data.b + 1
        })
      } else if(k==2){
        imgs.push(res.data.path)
        that.setData({
          imgs: imgs,
          b: that.data.b + 1
        })
      }else{
        imgss.push(res.data.path)
        that.setData({
          imgss: imgss,
          b: that.data.b + 1
        })
      }
    }, err => {
      console.log(err)
      // HError 对象
    })

  },

  /**
   * 提交照片和视频
   * @param {*} e 
   */
  ups: function () {
    var that = this
    return new Promise((resolve, reject) => {
      for (var i = 0; i < that.data.imgList.length; i++) {
        that.up(that.data.imgList[i], 1)
      }
      for (var j = 0; j < that.data.imgLists.length; j++) {
        that.up(that.data.imgLists[j], 2)
      }

      for (var k = 0; k < that.data.imgListss.length; k++) {
        that.up(that.data.imgListss[k], 3)
      }
      var ui = setInterval(function (e) {
        console.log(that.data.b)
        if (that.data.b == 3) {
          resolve(1)
          clearInterval(ui)
        }
      }, 1000)
    })
  },
  /**
   * 提交
   * @param {*} e 
   */
  from: async function () {
    var that = this
    wx.showToast({
      title: '上传中...',
      mask: true,
      icon: 'loading'
    })
    var a = await that.ups();
    // 向 tableName 为 'product' 的数据表插入一条记录
    if (a == 1) {
      let tableName = 'gaoya'
      let Product = new wx.BaaS.TableObject(tableName)
      let product = Product.create()
      var date = new Date()
      var year = date.getFullYear();
      var mouth = date.getMonth() + 1;
      var day = date.getDate()
      // 设置方式二
      var item = []
      item.push(that.data.img[0])
      item.push(that.data.imgs[0])
      console.log(item)
      if(that.data.i == 0){
        product.set('diankong',item)
        product.set('upID', that.data.upID)
        product.set('downID', that.data.downID)
      }
      if(that.data.i == 1){
        product.set('dianchi',item)
        product.set('upID', that.data.upID)
        product.set('downID', that.data.downID)
      }
      if(that.data.i == 2){
        product.set('qudong',item)
        product.set('vin', that.data.upID)
        product.set('fadong', that.data.downID)
      }
      product.set('leixing', that.data.leixing)
      product.set('car', that.data.imgss[0])
      product.set('carID', that.data.carID)
      product.set('shijian', year + '.' + mouth + '.' + day)
      product.save().then(res => {
        // success
        wx.hideToast({})
        console.log(res)
        wx.showToast({
          title: '上传成功',
          icon:'success',
          duration:2000
        })
        wx.redirectTo({
          url: '../ingao/ingao',
        })
      }, err => {
        // HError 对象
      })


    }

  },
  /**
   * 选择换上码图片
   */
  ChooseImage() {
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },
  /**
   * 选择换下吗
   * @param {*} e 
   */
  ChooseImages() {
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgLists.length != 0) {
          this.setData({
            imgLists: this.data.imgLists.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgLists: res.tempFilePaths
          })
        }
      }
    });
  },
  /**
   * 选择车牌号
   */
  ChooseImagess() {
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgListss.length != 0) {
          this.setData({
            imgListss: this.data.imgListss.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgListss: res.tempFilePaths
          })
        }
      }
    });
  },
  /**
   * 删除举报图片
   * @param {}} e 
   */
  DelImg(e) {
    wx.showModal({
      content: '确定要删除？',
      cancelText: '不了',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  /**
   * 查看图片
   * @param {*} e 
   */
  ViewImage(e) {
    var items = []
    items.push(this.data.imgList[0])
    items.push(this.data.imgLists[0])
    items.push(this.data.imgListss[0])
    wx.previewImage({
      urls: items,
      current: e.currentTarget.dataset.url
    });
  },
  /**
   * 隐藏弹窗
   * @param {*} e 
   */
  hideModal() {
    this.setData({
      modalName: ''
    })
  },

})